<?php
/**
 * @version		$Id: gencode.php 21766 2011-07-08 12:20:23Z eddieajau $
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Gen Code component helper.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_gencode
 * @since		1.6
 */
class GenCodeHelper
{
	/**
	 * Configure the Linkbar.
	 *
	 * @param	string	The name of the active view.
	 *
	 * @return	void
	 * @since	1.6
	 */
	public static function addSubmenu($vName)
	{
		return true;
	}

	/**
	 * Gets a list of the actions that can be performed.
	 *
	 * @param	int		The category ID.
	 *
	 * @return	JObject
	 * @since	1.6
	 */
	public static function getActions($categoryId = 0)
	{
		$user	= JFactory::getUser();
		$result	= new JObject;

		if (empty($categoryId)) {
			$assetName = 'com_gencode';
		} else {
			$assetName = 'com_gencode.category.'.(int) $categoryId;
		}

		$actions = array(
			'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.state', 'core.delete'
		);

		foreach ($actions as $action) {
			$result->set($action,	$user->authorise($action, $assetName));
		}

		return $result;
	}
	
	/**
	 * Function to get component
	 * 
	 * @param Int $componentId Component ID
	 * @return Component Object
	 */
	public function getComponent($componentId)
	{
		$db = JFactory::getDbo();
		$query = "SELECT * FROM #__gencode_components WHERE id = '$componentId'";
		
		$db->setQuery($query);
		$result = $db->loadObject();
		
		return $result;
	}
	
	/**
	 * Function to define some tables in joomla db), and get tables user defined
	 * 
	 * @return Array of table
	 */
	public function getTables()
	{
		$tables = array(
							'categories' 		=> 'Categories',
							'languages' 		=> 'Languages',
							'users' 			=> 'Users',
							'usergroups' 		=> 'User Groups',
							'user_usergroup_map'=> 'User Group Map',
					);
					
		//get component db prefix
		$componentId = JFactory::getSession()->get('component_id');
		$component = GenCodeHelper::getComponent($componentId);
					
		//get tables user defined
		$db = JFactory::getDbo();
		$query = "SELECT * FROM #__gencode_gendbs WHERE component_id = '$componentId'";
		
		$db->setQuery($query);
		
		$rs = $db->loadObjectList();
		
		foreach ($rs as $row)
		{
			$tables[$row->name] = $row->name;
		}
		
		return $tables;
	}
	
	/**
	 * Function to get type of field. Ex: text, integer ...
	 */
	public function elementType()
	{
		$arr = array(
						'accesslevel' => array('title' => 'Access Level'),			
						'calendar' => array('title' => 'Calendar'),
						'category' => array('title' => 'Category'),
						'checkbox' => array('title' => 'Checkbox'),
						'checkboxes' => array('title' => 'Checkboxes'),
						'combo' => array('title' => 'Combo'),
						'editor' => array('title' => 'Editor'),
						'email' => array('title' => 'Email'),
						'file' => array('title' => 'File'),
						'hidden' => array('title' => 'Hidden'),
						'integer' => array('title' => 'Integer'),
						'language' => array('title' => 'Language'),
						'list' => array('title' => 'List'),
						'media' => array('title' => 'Media'),
						'password' => array('title' => 'Password'),
						'radio' => array('title' => 'Radio'),
						'spacer' => array('title' => 'Spacer'),
						'text' => array('title' => 'Text'),
						'textarea' => array('title' => 'Textarea'),
						'user' => array('title' => 'User'),
						'usergroup' => array('title' => 'User Group'),
						'-'			=> '-',
						'custom' => array('title' => 'Custom'),
		
				);
		
		return $arr;
	}
}
